package com.service;
import static com.def.GenericDef.ACTION_FLG_ADD;
import static com.def.GenericDef.ACTION_FLG_CANCEL;
import static com.def.GenericDef.ACTION_FLG_DELETE;
import static com.def.GenericDef.ACTION_FLG_FILTER_DISTINCT;
import static com.def.GenericDef.ACTION_FLG_QUERY;
import static com.def.GenericDef.ACTION_FLG_UPDATE;
import static com.def.GenericDef.RETURN_CODE_OK;
import static com.def.GenericDef.RETURN_CODE_UNKNOWN;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dao.IBisPamDao;
import com.icim.service.ICIMBaseService;
import com.model.Bis_pam;
import com.tx.Xpbispam.XpbispamI;
import com.tx.Xpbispam.XpbispamIA;
import com.tx.Xpbispam.XpbispamO;
import com.tx.Xpbispam.XpbispamOA;
import com.util.MqOpe;

@Scope("prototype")
@Service("XpbispamService")
public class XpbispamService extends ICIMBaseService{
	private Logger logger = Logger.getLogger(XpbispamService.class);
	@Autowired
	private XpbispamI inTrx;
	
	@Autowired
	private XpbispamO outTrx; 
	
	
	@Autowired
	private IBisPamDao bisPamdao;
	
	@Autowired
	private MqOpe mqOpe;
	public String subMainProc(String strInMsg){
		logger.info("InTrx is "+ strInMsg );
		Session session = bisPamdao.getCrSession();
		try{
			inTrx = (XpbispamI) mqOpe.inStr2Obj(strInMsg,inTrx);
			char action_flg = inTrx.getAction_flg().charAt(0);
			switch( action_flg ){
				case ACTION_FLG_QUERY           :  queryByCondition()    ; break;   
				case ACTION_FLG_ADD             :  addFnc()              ; break;
				case ACTION_FLG_UPDATE          :  updateFnc()           ; break;
				case ACTION_FLG_DELETE          :  deleteFnc()           ; break;
				case ACTION_FLG_FILTER_DISTINCT :  listDistinctFnc()     ; break;
				case ACTION_FLG_CANCEL          :  deleteByConditionFnc(); break;
				case 'K'                        :  getRealOneParam()     ; break;//查询实际使用的param
				default                         :  errActionFnc();   
			}
	        if( RETURN_CODE_OK.equals(outTrx.getRtn_code()) ){
	        	session.flush();
			}else{
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}		
			
		}
		catch(Exception ex){
			outTrx.setTrx_id("XPBISPAM");
			outTrx.setType_id("O");
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			outTrx.setRtn_mesg(ex.getCause().toString());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			//TODO:throw dbRollBackException
		}finally{
			logger.info(mqOpe.obj2Str(outTrx));
		}
		return mqOpe.obj2Str(outTrx);
	}
	private void getRealOneParam() {

		/***
		 * 支持
		 *   
		 *   by bay_id,wo_id_fk,ope_id_fk 查询
		 *   
		 * **/
		 
		outTrx.setTrx_id("XPBISPAM");
		outTrx.setType_id("O");
		List<XpbispamOA> oaryList = new ArrayList<XpbispamOA> ();
		
		
		XpbispamIA iary = inTrx.getIary().get(0);
		String tool_id_fk=iary.getTool_id_fk();
//		String mdl_typ_fk=iary.getMdl_typ_fk();
		String mdl_id_fk=iary.getMdl_id_fk();
		String path_id_fk=iary.getPath_id_fk();
		String path_ver_fk=iary.getPath_ver_fk();
		String ope_no_fk=iary.getOpe_no_fk();
		String ope_id_fk=iary.getOpe_id_fk();
		String ope_ver_fk=iary.getOpe_ver_fk();
		
		StringBuffer hql = new StringBuffer();
		hql.append("FROM Bis_pam where 1=1 ");
		hql.append(" AND tool_id_fk ='").append(tool_id_fk).append("' ");
//		hql.append(" AND mdl_typ_fk in('*','").append(mdl_typ_fk).append("') ");
		hql.append(" AND mdl_id_fk in('*','").append(mdl_id_fk).append("') ");
		hql.append(" AND path_id_fk in('*','").append(path_id_fk).append("') ");
		hql.append(" AND path_ver_fk in('*','").append(path_ver_fk).append("') ");
		hql.append(" AND ope_no_fk in('*','").append(ope_no_fk).append("') ");
		hql.append(" AND ope_id_fk in('*','").append(ope_id_fk).append("') ");
		hql.append(" AND ope_ver_fk in('*','").append(ope_ver_fk).append("') ");
		hql.append(" order by mdl_id_fk desc,path_id_fk desc,path_ver_fk desc,");
		hql.append(" ope_no_fk desc,ope_id_fk desc,ope_ver_fk desc");
		List<Bis_pam> bisPamList =bisPamdao.find( hql.toString() );
		
		oaryList.clear();
		XpbispamOA oary = new XpbispamOA();
		if(bisPamList.size()>=1){
			Bis_pam bis_pam = bisPamList.get(0);
			oary.setTool_id_fk(bis_pam.getId().getTool_id_fk());
			oary.setMdl_typ_fk(bis_pam.getId().getMdl_typ_fk());
			oary.setMdl_id_fk(bis_pam.getId().getMdl_id_fk());
			oary.setPath_id_fk(bis_pam.getId().getPath_id_fk());
			oary.setPath_ver_fk(bis_pam.getId().getPath_ver_fk());
			oary.setOpe_id_fk(bis_pam.getId().getOpe_id_fk());
			oary.setOpe_no_fk(bis_pam.getId().getOpe_no_fk());
			oary.setOpe_ver_fk(bis_pam.getId().getOpe_ver_fk());
			oary.setRecipe_id(bis_pam.getRecipe_id());
			oary.setActive_flg(bis_pam.getActive_flg());
			oary.setSkip_flg(bis_pam.getSkip_flg());
			oary.setMes_id(bis_pam.getMes_id());
			oary.setBox_set_code(bis_pam.getBox_set_code());
			oary.setGd_judge_flg(bis_pam.getGd_judge_flg());
			oary.setDef_judge_flg(bis_pam.getDef_judge_flg());
			oary.setSwh_cnt_add_flg(bis_pam.getSwh_cnt_add_flg());
			oary.setSwh_cnt_add_pos(bis_pam.getSwh_cnt_add_pos());
			oary.setSwh_max_cnt(bis_pam.getSwh_max_cnt());
			oaryList.add(oary);
		}
		outTrx.setOary(oaryList);
		outTrx.setTbl_cnt(oaryList.size());
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg("success");
		
	}
//	private Bis_pam getOnePam(List<Bis_pam> bisPamList) {
//		int pamCnt = bisPamList.size();
//		List<Bis_pam> linkedList = new LinkedList<Bis_pam>();
//		for(Bis_pam bis_pam:bisPamList){
//			linkedList.add(bis_pam);
//		}
//		
//		for(int i=0;i<linkedList.size();i++){
//			while(linkedList.size()<=1){
//				break;
//			}
//			/**
//			 * 过滤mdl_id_fk是*的
//			 */
//			for(int j=0;j<pamCnt;j++){
//				if(linkedList.get(i).getId().getMdl_id_fk().equals("*")){
//					linkedList.remove(i);
//					break;
//				}
//			}
//			
//		}
//		return linkedList.get(0);
//	}
	public void addFnc(){
		outTrx = bisPamdao.addProc(inTrx);
	}
	public void updateFnc(){
		outTrx = bisPamdao.updateProc(inTrx);
	}
	public void deleteFnc(){
		outTrx = bisPamdao.deleteProc(inTrx);
	}
	public void queryByCondition(){
		outTrx = bisPamdao.queryByCondition(inTrx);
	}
	public void listDistinctFnc(){
		outTrx = bisPamdao.listDistinctFnc(inTrx);
	} 
	public void deleteByConditionFnc(){
		outTrx = bisPamdao.deleteByConditionFnc(inTrx);
	}
	public void errActionFnc(){
		outTrx.setTrx_id("XPBISPAM");
		outTrx.setType_id("O");
		outTrx.setRtn_code("0000009");
		outTrx.setRtn_mesg("Invalid Action_flg");
	}
	
}
